/*
 * Copyright (c) 2023 vitasystems GmbH
 *
 * This file is part of project EHRbase Migration Tool
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.ehrbase.migration.exporter.v0.jooq.pg.tables;

import java.util.UUID;
import java.util.function.Function;
import org.ehrbase.migration.exporter.v0.jooq.pg.Ehr;
import org.ehrbase.migration.exporter.v0.jooq.pg.tables.records.EhrStatusRecord;
import org.jooq.Field;
import org.jooq.ForeignKey;
import org.jooq.Function11;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.Records;
import org.jooq.Row11;
import org.jooq.Schema;
import org.jooq.SelectField;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableOptions;
import org.jooq.impl.DSL;
import org.jooq.impl.SQLDataType;
import org.jooq.impl.TableImpl;

/**
 * This class is generated by jOOQ.
 */
@SuppressWarnings({"all", "unchecked", "rawtypes"})
public class EhrStatus extends TableImpl<EhrStatusRecord> {

    private static final long serialVersionUID = 1L;

    /**
     * The reference instance of <code>ehr.ehr_status</code>
     */
    public static final EhrStatus EHR_STATUS = new EhrStatus();

    /**
     * The class holding records for this type
     */
    @Override
    public Class<EhrStatusRecord> getRecordType() {
        return EhrStatusRecord.class;
    }

    /**
     * The column <code>ehr.ehr_status.id</code>.
     */
    public final TableField<EhrStatusRecord, UUID> ID = createField(DSL.name("id"), SQLDataType.UUID, this, "");

    /**
     * The column <code>ehr.ehr_status.name</code>.
     */
    public final TableField<EhrStatusRecord, String> NAME = createField(DSL.name("name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.ref</code>.
     */
    public final TableField<EhrStatusRecord, String> REF = createField(DSL.name("ref"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.scheme</code>.
     */
    public final TableField<EhrStatusRecord, String> SCHEME =
            createField(DSL.name("scheme"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.namespace</code>.
     */
    public final TableField<EhrStatusRecord, String> NAMESPACE =
            createField(DSL.name("namespace"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.type</code>.
     */
    public final TableField<EhrStatusRecord, String> TYPE = createField(DSL.name("type"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.id_value</code>.
     */
    public final TableField<EhrStatusRecord, String> ID_VALUE =
            createField(DSL.name("id_value"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.issuer</code>.
     */
    public final TableField<EhrStatusRecord, String> ISSUER =
            createField(DSL.name("issuer"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.assigner</code>.
     */
    public final TableField<EhrStatusRecord, String> ASSIGNER =
            createField(DSL.name("assigner"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.type_name</code>.
     */
    public final TableField<EhrStatusRecord, String> TYPE_NAME =
            createField(DSL.name("type_name"), SQLDataType.CLOB, this, "");

    /**
     * The column <code>ehr.ehr_status.party</code>.
     */
    public final TableField<EhrStatusRecord, UUID> PARTY = createField(DSL.name("party"), SQLDataType.UUID, this, "");

    private EhrStatus(Name alias, Table<EhrStatusRecord> aliased) {
        this(alias, aliased, null);
    }

    private EhrStatus(Name alias, Table<EhrStatusRecord> aliased, Field<?>[] parameters) {
        super(
                alias,
                null,
                aliased,
                parameters,
                DSL.comment(""),
                TableOptions.view(
                        """
        create view "ehr_status" as  SELECT ehr.id,
         party.name,
         party.party_ref_value AS ref,
         party.party_ref_scheme AS scheme,
         party.party_ref_namespace AS namespace,
         party.party_ref_type AS type,
         identifier.id_value,
         identifier.issuer,
         identifier.assigner,
         identifier.type_name,
         identifier.party
        FROM (((ehr.ehr ehr
          JOIN ehr.status status ON ((status.ehr_id = ehr.id)))
          JOIN ehr.party_identified party ON ((status.party = party.id)))
          LEFT JOIN ehr.identifier identifier ON ((identifier.party = party.id)));
        """));
    }

    /**
     * Create an aliased <code>ehr.ehr_status</code> table reference
     */
    public EhrStatus(String alias) {
        this(DSL.name(alias), EHR_STATUS);
    }

    /**
     * Create an aliased <code>ehr.ehr_status</code> table reference
     */
    public EhrStatus(Name alias) {
        this(alias, EHR_STATUS);
    }

    /**
     * Create a <code>ehr.ehr_status</code> table reference
     */
    public EhrStatus() {
        this(DSL.name("ehr_status"), null);
    }

    public <O extends Record> EhrStatus(Table<O> child, ForeignKey<O, EhrStatusRecord> key) {
        super(child, key, EHR_STATUS);
    }

    @Override
    public Schema getSchema() {
        return aliased() ? null : Ehr.EHR;
    }

    @Override
    public EhrStatus as(String alias) {
        return new EhrStatus(DSL.name(alias), this);
    }

    @Override
    public EhrStatus as(Name alias) {
        return new EhrStatus(alias, this);
    }

    @Override
    public EhrStatus as(Table<?> alias) {
        return new EhrStatus(alias.getQualifiedName(), this);
    }

    /**
     * Rename this table
     */
    @Override
    public EhrStatus rename(String name) {
        return new EhrStatus(DSL.name(name), null);
    }

    /**
     * Rename this table
     */
    @Override
    public EhrStatus rename(Name name) {
        return new EhrStatus(name, null);
    }

    /**
     * Rename this table
     */
    @Override
    public EhrStatus rename(Table<?> name) {
        return new EhrStatus(name.getQualifiedName(), null);
    }

    // -------------------------------------------------------------------------
    // Row11 type methods
    // -------------------------------------------------------------------------

    @Override
    public Row11<UUID, String, String, String, String, String, String, String, String, String, UUID> fieldsRow() {
        return (Row11) super.fieldsRow();
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Function)}.
     */
    public <U> SelectField<U> mapping(
            Function11<
                            ? super UUID,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super UUID,
                            ? extends U>
                    from) {
        return convertFrom(Records.mapping(from));
    }

    /**
     * Convenience mapping calling {@link SelectField#convertFrom(Class,
     * Function)}.
     */
    public <U> SelectField<U> mapping(
            Class<U> toType,
            Function11<
                            ? super UUID,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super String,
                            ? super UUID,
                            ? extends U>
                    from) {
        return convertFrom(toType, Records.mapping(from));
    }
}
